package com.markul.android.ui.personal

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.markul.android.MarkulApplication
import com.markul.android.R
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import com.markul.android.ui.mark.OnAblumItemClickListener

class AlbumAdapter(private val picUrlList: ArrayList<String?>,private val number:ArrayList<Int>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var onPicClick: OnAblumItemClickListener? = null


    inner class picViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val picImage: ImageView = itemView.findViewById(R.id.picImage)
        val numberText:TextView=itemView.findViewById(R.id.photoNum)
    }

    inner class picOnlyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val picImage: ImageView = itemView.findViewById(R.id.picImage)
    }

    inner class timeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val picTime: TextView = itemView.findViewById(R.id.picTime)
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
         if (viewType == 0) {
            view = LayoutInflater.from(MarkulApplication.context)
                .inflate(R.layout.item_album_time, parent, false)
             view.setBackgroundColor(0)
             return timeViewHolder(view)
        }
        else if(viewType == 1){
            view= LayoutInflater.from(MarkulApplication.context)
                .inflate(R.layout.item_album_picture_only, parent, false)
             view.setBackgroundColor(0)
             return picOnlyViewHolder(view)
        }
        else if(viewType==2){
            view = LayoutInflater.from(MarkulApplication.context)
                .inflate(R.layout.item_album_picture, parent, false)
             view.setBackgroundColor(0)
             return picViewHolder(view)
        }
        else{
             view = LayoutInflater.from(MarkulApplication.context)
                 .inflate(R.layout.item_album_picture, parent, false)
            return Holder(view)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is timeViewHolder) {
            holder.picTime.text = picUrlList[position]
        } else if (holder is picViewHolder) {
            Glide.with(holder.itemView.context).load(picUrlList[position].toString())
                .into(holder.picImage)
            holder.numberText.text=number[position].toString()
            holder.picImage.setOnClickListener {
                onPicClick?.onPicsClick(position)
            }
        }else if(holder is picOnlyViewHolder){
            Glide.with(holder.itemView.context).load(picUrlList[position].toString())
                .into(holder.picImage)
            holder.picImage.setOnClickListener {
                onPicClick?.onPicOnlyClick(position)
            }
        }

    }

    override fun getItemCount() = picUrlList.size

    override fun getItemViewType(position: Int): Int {
        return if (picUrlList[position]?.get(4) == '年'||picUrlList[position]?.get(0)=='你') {
            0
        } else {
            if(number[position]==1){
                1
            }
            else if(number[position]==0){
                3
            }
            else{
                2
            }
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val manager = recyclerView.layoutManager
        if (manager is GridLayoutManager) {
            manager.spanSizeLookup = object : SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return if (getItemViewType(position) == 0) manager.spanCount else 1
                }
            }
        }
    }

    fun setOnMyClickListener(onClickListener: OnAblumItemClickListener?
    ) {
        onPicClick= onClickListener
    }

}